package com.gem.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Author: Xhy
 * CreateTime: 2022-09-29 14:47
 */
public class Page<T> {
    public static void main(String[] args) {
        ArrayList<Integer> data = new ArrayList<>();
        data.add(1);
        data.add(2);
        data.add(3);
        data.add(4);
        data.add(5);
        data.add(6);
        Page<Integer> page = new Page<Integer>().page(data, 3, 2);
        System.out.println(page.getData());
        System.out.println(page.isPre());
        System.out.println(page.isNext());
        System.out.println(page.getPageSize());

    }

    // 数据
    private List<T> data;

    // 当前页
    private long currentPage;

    // 每页记录数
    private long limit;

    // 是否有上一页
    private boolean pre;

    // 是否有下一页
    private boolean next;

    // 总页数
    private long pageSize;

    // 所有数据大小
    private int size;

    /**
     * 分页
     *
     * @param data        数据
     * @param currentPage 当前页
     * @param limit       每页记录数
     * @return
     */
    public Page<T> page(List<T> data, long currentPage, long limit) {
        Page<T> page = new Page<>();

        if (data == null || data.size() == 0) {
            return page;
        }

        // 所有数据大小
        int size = data.size();
        // 求出总页数(总大小%每页记录数 == 0 ? 总大小/每页记录数 :总大小/每页记录数 +1) 如果有余数那就要多加1页
        long pageSize = size % limit == 0 ? size / limit : (size / limit) + 1;
        // 安全限制
        if (currentPage <= 0){
            page.currentPage = 1;
        }else if (currentPage > pageSize){
            page.currentPage = pageSize;
        }else {
            page.currentPage =currentPage;
        }
        page.limit = limit;
        page.next = currentPage != pageSize;
        page.pre = currentPage != 1;
        page.size = size;
        page.pageSize = pageSize;
        // (当前页- 1) * limit
        page.data = data.stream().skip((currentPage - 1) * limit).limit(limit).collect(Collectors.toList());
        return page;
    }

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    public long getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(long currentPage) {
        this.currentPage = currentPage;
    }

    public long getLimit() {
        return limit;
    }

    public void setLimit(long limit) {
        this.limit = limit;
    }

    public boolean isPre() {
        return pre;
    }

    public void setPre(boolean pre) {
        this.pre = pre;
    }

    public boolean isNext() {
        return next;
    }

    public void setNext(boolean next) {
        this.next = next;
    }

    public long getPageSize() {
        return pageSize;
    }

    public void setPageSize(long pageSize) {
        this.pageSize = pageSize;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }
}
